<template>
  <el-row type="flex" justify="center">
    <el-form ref="formData" :model="formData" :rules="rules" label-width="80px" @keyup.enter.native="register()">
      <el-form-item prop="userName" label="用户名">
        <el-input v-model="formData.userName" placeholder="请输入用户名" prefix-icon="icon-login_user"
                  clearable></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input v-model="formData.password" placeholder="请输入密码" type="password" prefix-icon="icon-login_pwd"
                  clearable></el-input>
      </el-form-item>
      <el-form-item prop="checkPassword" label="确认密码">
        <el-input v-model="formData.checkPassword" placeholder="再次输入密码" type="password"
                  prefix-icon="icon-login_pwd" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="formData.role" placeholder="请选择你要注册的身份">
          <el-option label="教师" value="教师"/>
          <el-option label="学生" value="学生"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="register('formData')" icon="el-icon-upload">注册</el-button>
        <el-button @click="resetForm('formData')">重置</el-button>
      </el-form-item>
      <router-link to="login">已有密码？登录</router-link>

    </el-form>
  </el-row>
</template>
<script>
import axios from 'axios'

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.formData.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      formData: {
        userName: '',
        password: '',
        checkPassword: '',
        role: ''
      },
      rules: {
        userName: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
        password: [{required: true, message: '密码不能为空', trigger: 'blur'}],
        checkPassword: [{required: true, validator: validatePass, trigger: 'blur'}]

      },
      options: ["教师", "学生"],
    };
  },
  methods: {
    register(formName) {
      const that = this
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.formData.role === "教师") {
            axios.post('/teacher', {account: this.formData.userName, password: this.formData.password})
                .then(function (res) {
                  if (res.data.code === 200) {
                    alert("注册成功！")
                    that.$router.push({name: 'login'});
                  } else {
                    alert('注册失败!!');
                    return false;
                  }
                })
          }
          if (this.formData.role === "学生") {
            axios.post('/student', {account: this.formData.userName, password: this.formData.password})
                .then(function (res) {
                  if (res.data.code === 200) {
                    alert("注册成功！")
                    that.$router.push({name: 'login'});
                  } else {
                    alert('注册失败!!');
                    return false;
                  }
                })
          }
        }
      });
    },
    resetForm(formName) {
      const that = this
      that.$refs[formName].resetFields();
    }
  }
};
</script>